<?php
/**************************************************************************
	ESAM (EVE to SMF API Module)
	Copyright (C) 2009  Clinton Weiss

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

include_once("EsamQuery.php");
include_once("EsamGlobal.php");

class EsamMembers
{
	function getMemberName($member_id)
	{
		$queryFetcher = new EsamQuery();
		$queryFetcher->setQuery($queryFetcher->Q_MEMBER_INFO);
		$queryFetcher->setParameter("member_id", $member_id);
		return $queryFetcher->getResultSetValue("memberName");
	}

	function getMemberSetting($member_id, $name, $default)
	{
		$queryFetcher = new EsamQuery();
		$queryFetcher->setQuery($queryFetcher->Q_MEMBER_SETTING);
		$queryFetcher->setParameter("name", $name);
		$queryFetcher->setParameter("member_id", $member_id);
		$value = $queryFetcher->getResultSetValue("VALUE");
		if (empty($value))
		{
			return $default;
		}
		return $value;
	}

	function setMemberSetting($member_id, $name, $value)
	{
		// Delete the existing row first.
		$this->removeMemberSetting($member_id, $name);

		// And now (re)insert
		$queryFetcher = new EsamQuery();
		$queryFetcher->setQuery($queryFetcher->I_MEMBER_SETTING);
		$queryFetcher->setParameter("name", $name);
		$queryFetcher->setParameter("value", $value);
		$queryFetcher->setParameter("member_id", $member_id);
		$queryFetcher->executeQuery();
	}

	function removeMemberSetting($member_id, $name)
	{
		$queryFetcher = new EsamQuery();
		$queryFetcher->setQuery($queryFetcher->D_MEMBER_SETTING);
		$queryFetcher->setParameter("name", $name);
		$queryFetcher->setParameter("member_id", $member_id);
		$queryFetcher->executeQuery();
	}

	function getEveUserData($member_id)
	{
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->Q_EVE_API);
		$esamQuery->setParameter("member_id", $member_id);
		return $esamQuery->getResultSet();
	}

	function saveEveUserData($member_id, $user_id, $api_key)
	{
		$this->removeEveUserData($member_id);

		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->I_EVE_API_INSERT);
		$esamQuery->setParameter("member_id", $member_id);
		$esamQuery->setParameter("user_id", $user_id);
		$esamQuery->setParameter("api_key", $api_key);
		$esamQuery->executeQuery();
	}

	function removeEveUserData($member_id)
	{
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->D_EVE_API_DELETE);
		$esamQuery->setParameter("member_id", $member_id);
		$esamQuery->executeQuery();
	}

	/**
	 * Checks to see if the given userid and api key have already been
	 * claimed by a user.  Will return true if the api has already
	 * been entered previously.
	 *
	 * @param <type> $user_id
	 * @param <type> $api_key
	 * @return <type>
	 */
	function isApiAlreadyEntered($user_id, $api_key)
	{
		$queryFetcher = new EsamQuery();
		$queryFetcher->setQuery($queryFetcher->Q_EVE_API_SEARCH);
		$queryFetcher->setParameter("user_id", $user_id);
		$queryFetcher->setParameter("api_key", $api_key);
		$result = $queryFetcher->getResultSet();
		return $result->size() > 0;
	}


	function clearMemberFromMemberGroups($member_id, $memberGroups)
	{
		// Get the user's current groups
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->Q_MEMBER_INFO);
		$esamQuery->setParameter("member_id", $member_id);
		$membersMemberGroups = $esamQuery->getResultSetValue("additionalGroups");

		// Split it up into a real array
		$membersMemberGroups = split(',', $membersMemberGroups);

		// Remove the groups that have rules defined
		$noRulesMemberGroups = array_diff($membersMemberGroups, $memberGroups);

		// Update the user
		// Get the user's current groups
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->U_MEMBERS_GROUPS);
		$esamQuery->setParameter("member_id", $member_id);
		$esamQuery->setParameter("member_groups", implode(',', $noRulesMemberGroups));
		$esamQuery->executeQuery();

		updateStats('postgroups', 'ID_MEMBER = ' .  $member_id);
	}

	function addMemberToMemberGroup($member_id, $id_group)
	{
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery($esamQuery->U_MGROUP_ADD);
		$esamQuery->setParameter("member_id", $member_id);
		$esamQuery->setParameter("id_group", $id_group);
		$esamQuery->executeQuery();

		updateStats('postgroups', 'ID_MEMBER = ' .  $member_id);
	}
}
?>
